package com.gcl.rabbitmq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {

    public static  final String QUEUE_NAME = "Hello Word";

    public static void main(String[] args) throws IOException, TimeoutException {

        // 创建连接配置类
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setUsername("anoraker");
        factory.setPassword("anoraker");

        // 创建连接
        Connection connection = factory.newConnection();

        // 创建信道
        Channel channel = connection.createChannel();

        /**
         * 消费者消费信息
         * 1. 消费的队列名称
         * 2. 消费成功后是够要自动应答 true 自动应答
         * 3. 消费者消费未成功的回调
         * 4. 消费者录取消息的回调
         */
        // 正确消费的回调
        DeliverCallback deliverCallback=(consumerTag,delivery) -> {
            String message= new String(delivery.getBody());
            System.out.println(message);
        };
        //消费被中断
        CancelCallback cancelCallback=(consumerTag)->{
            System.out.println("消息消费被中断");
        };


        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);

    }
}
